package linear_list.leetcode.medium;

/**
 * @author bruin_du
 * @description 删除链表的倒数第 n 个结点
 * @date 2022/8/5 23:45
 **/
public class OfferII021_RemoveNthFromEnd {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null || head.next == null) return null;
        ListNode dummyHead = new ListNode(0, head);
        ListNode prev = dummyHead, slow = head, fast = head;
        for (int i = 0; i < n; i++) {
            fast = fast.next;
        }
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
            prev = prev.next;
        }
        prev.next = slow.next;
        return dummyHead.next;
    }
}
